package com.ticket.sass.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.CinemaExtendMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.CinemaExtendEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 影院扩展表 服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-03-06
 */
@Service
public class CinemaExtendServiceImpl extends ServiceImpl<CinemaExtendMapper, CinemaExtendEntity> implements BaseService<CinemaExtendEntity> {
    public void updateContactByCinemaId(Long cinemaId, String newContact) {
        UpdateWrapper<CinemaExtendEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("cinema_id", cinemaId);
        updateWrapper.set("contact", newContact);
        update(updateWrapper);
    }
    public void updateContactByCinemaId(Long cinemaId, String newContact, Long tenantId) {
        // 查询是否存在记录
        QueryWrapper<CinemaExtendEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cinema_id", cinemaId);
        queryWrapper.eq("tenant_id", tenantId);
        CinemaExtendEntity cinemaExtendEntity = getOne(queryWrapper);
        
        if (cinemaExtendEntity != null) {
            // 存在记录，更新 contact 字段
            UpdateWrapper<CinemaExtendEntity> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("cinema_id", cinemaId);
            updateWrapper.eq("tenant_id", tenantId);
            updateWrapper.set("contact", newContact);
            update(updateWrapper);
        } else {
            // 不存在记录，新增一条
            CinemaExtendEntity newCinemaExtendEntity = new CinemaExtendEntity();
            newCinemaExtendEntity.setCinemaId(cinemaId);
            newCinemaExtendEntity.setTenantId(tenantId);
            newCinemaExtendEntity.setContact(newContact);
            save(newCinemaExtendEntity);
        }
    }

    /**
     * 根据影院ID列表查询对应的影院扩展信息，并返回一个Map。
     * @param cinemaIds 影院ID列表
     * @return 包含影院扩展信息的Map，键为影院ID，值为对应的影院扩展实体对象
     */
    public Map<Long, CinemaExtendEntity> getCinemaExtendMapByCinemaId(List<Long> cinemaIds) {
        if (cinemaIds.size() == 0){
            return new HashMap<>();
        }
        Map<Long, CinemaExtendEntity> resultMap = new HashMap<>();
        // 根据影院ID列表查询对应的影院扩展信息
        List<CinemaExtendEntity> cinemaExtendList = list(new QueryWrapper<CinemaExtendEntity>().in("cinema_id", cinemaIds));
        // 将查询结果转换为Map
        for (CinemaExtendEntity cinemaExtendEntity : cinemaExtendList) {
            resultMap.put(cinemaExtendEntity.getCinemaId(), cinemaExtendEntity);
        }
        return resultMap;
    }
}
